﻿;(function($){
	$.pagination = function (options) {
		var defaults = {
			mainClass: '',
			pageClass: 'pageNum', // 普通也的样式
			currentPageClass: '', // 当前页的样式
			pageFirstClass: '', // 第一页的样式
			pagePreviewClass: '', // 上一页的样式
			pageNextClass: '', // 下一页的样式
			pageLastClass: '', // 最后一页的样式
			pageParamName : 'page', // 分页的参数
			maxShowPages : 10,
			pageClickFunction : null,
			pageFirstLabel: '首页',
			pagePreviewLabel: '上一页',
			pageNextLabel: '下一页',
			pageLastLabel: '末页',
			id: '',
			data: {},
			url: '',
		};
		var options = $.extend(defaults, options);
		var queryStr = '';
		var $this = $('#' + options.id);
		console.log($this);

		function setQuery(data, pageParamName) {
			var queryStrs = [];
			options.data = data;
			if(pageParamName != undefined){
				options.pageParamName = pageParamName;
			}
			for (var i in data) {
				if(i !== options.pageParamName){
					var value = data[i];
					queryStrs.push(i + "=" + data[i]);
				}
			}
			queryStr = "?" + "&".join(queryStrs);
		}


		function pageClick() {
			if(pageClickFunction instanceof Function){
				pageClickFunction($(this).attr('href'));
				return false;
			}
			return true;
		}

		function generatePageAction(page, name, pageClass) {
			return '<a href="' +
						options.url + queryStr + (!!queryStr ? '&' : '') + options.pageParamName + '=' + page +
				   '" class="' + pageClass + '">'
					+ name + '</a>';
		}

		function generatePagingaction(currentPage, total) {
			var pageStart = 1;
			var pageEnd = total;
			if (currentPage > total) {
				currentPage = total;
			}
			if (total > options.maxShowPages) {
				half = Math.ceil(options.maxShowPages / 2);
				if (currentPage > half) {
					pageStart = currentPage - half;
					pageEnd = currentPage + half;
					if (pageEnd > total) {
						pageEnd = total;
						pageStart = total - options.maxShowPages;
						pageStart = pageStart > 0 ? pageStart : 1;
					}
				} else {
					pageStart = 1;
					pageEnd = options.maxShowPages;
				}
				while (pageEnd - pageStart >= options.maxShowPages) {
					pageEnd--;
				}
			}
			var pages = [];
			if (pageStart > 1) {
				pages.push({
					name: options.pageFirstLabel,
					pageClass: options.pageFirstClass,
					page: 1
				});
				pages.push({
					name: options.pagePreviewLabel,
					pageClass: options.pagePreviewClass,
					page: currentPage - 1
				});
			}
			for (var page = pageStart; page <= pageEnd; page++) {
				pages.push({
					name: page + "",
					page: page,
					pageClass: options.pageClass + (page == currentPage ? ' ' + options.currentPageClass : '')
				});
			}
			console.log(pageEnd);
			if (pageEnd != total) {
				pages.push({
					name: options.pageNextLabel,
					pageClass: options.pageNextClass,
					page: currentPage + 1
				});
				pages.push({
					name: options.pageLastLabel,
					pageClass: options.pageLastClass,
					page: total
				});
			}
			var html = '';
			for (var i in pages) {
				var item = pages[i];
				html += generatePageAction(item.page, item.name, item.pageClass);
			}
			$this.html('<div class="' + options.mainClass + '">' + html + '</div>');
		}

		$(document).on('click', '.' + options.pageClass, pageClick);

		return {
			set: generatePagingaction,
			setQuery: setQuery,
		};
	}
})(jQuery);